package cn.sieis.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.sieis.pojo.Admin;
import cn.sieis.service.admin.AdminService;
import cn.sieis.tools.Constants;

@Controller
public class LoginFrontController {

	@Autowired
	private AdminService adminService;

	@RequestMapping(value = "/front/login.html", method = RequestMethod.GET)
	public String login(@ModelAttribute("admin") Admin admin) throws Exception {
		return "front/login";
	}

	@RequestMapping(value = "/front/savelogin.html", method = RequestMethod.POST)
	public String frontlogin(@RequestParam String adminCode, @RequestParam String adminPassword, HttpServletRequest request, HttpSession session) throws Exception {
		Admin admin = adminService.login(adminCode, adminPassword);
		if (null != admin) {
			session.setAttribute(Constants.ADMIN_SESSION, admin);
			return "front/index";
		} else {
			request.setAttribute("error", "用户名或密码不正确");
			return "front/login";
		}
	}

	@RequestMapping(value = "/test")
	public String testF2F() {
		return "front/index";

	}

	@RequestMapping(value = "front/dologin.html", method = RequestMethod.GET)
	public String loginback(@ModelAttribute("admin") Admin admin) throws Exception {
		return "back/login";
	}

}
